<?php

/*	Vulpes-backend - Web-based managegent for wireless internet providers.
	Copyright (C) 2007 LinFox Serviços em Informática Ltda.

	This file is part of the Vulpes backend.

	Vulpes-backend is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Vulpes-backend is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 	Please note that any front-end (client application) that is distributed
	along with the Vulpes back-end, such as the Vulpes front-end by LinFox,
	is subject to it's correspending licene. */

class contract
{
	public $handle;
	public $client_username;
	public $plan_id;
	public $ping = 0;
	public $ip;
	public $mac;
	public $status;
	public $exists = false;
	private $db;

	function __construct($handle = false)
	{
		global $db;
		$this->db =& $db;

		if ($handle) {
			$contract_sql = '
				SELECT *
				FROM `contracts`
				WHERE `handle` = "'.$handle.'"';
			if (!$contract_result = $this->db->query($contract_sql)) return false;
			elseif ($contract_result->num_rows < 1) return false;
			$contract = $contract_result->fetch_object();
			$contract_result->free();
			foreach ($contract as $key => $value) $this->$key = $value;
			$this->ping = $this->ping();
			$this->exists = true;
			return true;
		}
	}
	
	function insert()
	{
		global $page;

		$sql_save = '
			INSERT INTO `contracts`
			SET	`client_username` = "' .$this->client_username. '",
				`plan_id` = "' .$this->plan_id. '",
				`mac` = "' .$this->mac. '",
				`ip` = "' .$this->ip. '",
				`status` = "' .$this->status. '"';
		if (!$this->db->query($sql_save)) {
			$page->error('{lang_changes_not_saved}');
			return false;
		} else {
			$page->notify('{lang_changes_saved}');
			return true;
		}
	}

	function update() {
		global $page;

		if (!$this->exists) {
			$page->error('{lang_contract_doesnt_exist}');
			return false;
		}
		$sql_update = '
			UPDATE `contracts`
			SET `plan_id` = "'.$this->plan_id.'",
				`mac` = "'.$this->mac.'",
				`ip` = "'.$this->ip.'",
				`status` = "'.$this->status.'"
			WHERE `handle` = "'.$this->handle.'"';
		if (!$this->db->query($sql_update)) {
			$page->error('{lang_changes_not_saved}');
			return false;
		}
		else {
			$page->notify('{lang_changes_saved}');
			return true;
		}
	}

	function delete() {
		global $page;

		if (!$this->exists) {
			$page->error('{lang_contract_doesnt_exist}');
			return false;
		}
		$delete_sql = 'DELETE FROM `contracts` WHERE `handle` = "'.$this->handle.'"';
		if (!$this->db->query($delete_sql)) {
			$page->error('{lang_not_deleted}');
			return false;
		} else {
			$page->notify('{lang_deleted}');
			return true;
		}
	}	
	
	function ping()
	{
		$arp = arp();
		foreach ($arp as $machine) {
			if ($this->status == 'L' && $machine['ip_address'] == $this->ip) {
				$ping_array = explode('/', exec('ping -q -c 1 '.$this->ip));
				return ereg_replace(' ms', '', $ping_array[4]);
			} else {
				return false;
			}
		}
	}
}

?>
